import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
import { z } from "zod";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

export function tryParseJSON(jsonString: string) {
  try {
    var json = JSON.parse(jsonString);
    return json;
  } catch (error) {
    return false;
  }
}

export function buildUrl(urlOrIp: string, forBrowser: boolean) {
  const ip = z.ipv4();

  let url = urlOrIp;
  if (ip.safeParse(urlOrIp).success) {
    url = `http://${urlOrIp}${forBrowser ? "" : ":8000"}`;
  }
  return url;
}

export function normalizeImageData(base64: string) {
  const prefix = "data:image/jpeg;base64,";
  return base64.startsWith(prefix) ? base64 : prefix + base64;
}
